package com.fenghongzhang.music.service;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;

import com.fenghongzhang.music.R;
import com.fenghongzhang.music.entity.Music;
import com.fenghongzhang.music.util.BroadcastConst;
import com.fenghongzhang.music.util.MusicModel;
import com.fenghongzhang.music.util.MusicUtils;

import java.io.IOException;
import java.util.List;
import java.util.Random;

public class MusicService extends Service {


    private static final String TAG = "MusicService";

    //暂存当前播放的下标
    private int index = 0;

    public static int model = 0;

    private MediaPlayer mediaPlayer;
    private List<Music> musicList;
    /**
     * 1,播放
     * 2,暂停
     * 3,上一首
     * 4,下一首
     * 5,加载数据源
     * 6,通知
     * 7,通知自定义布局
     *
     * 既能长期在后台运行,又能调用服务中的方法
     * 1,start 2 bind
     */

    public MusicService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();
        musicList = MusicUtils.getMusics(this);
    }



    //中间人的代理对象
    public class MyBinder extends Binder{
        public void callPlay(int position){
            play(position);
        }

        public void  callPause(){
            pause();
        }

        public void callRestart(){
            restart();
        }

        public void callPlayNext(){
            playNext();
        }
        public void callPlayPre(){
            palyPre();
        }

    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    //发送一个前台的通知
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(TAG, "onStartCommand: ");
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher); //不加,正常会报错.


        //z自定义布局的通知
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);


        //点击暂停按钮的逻辑
        Intent intent1 = new Intent();
        intent1.setAction(BroadcastConst.PAUSE);
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 111, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.notify_pause,broadcast);



        //继续播放
        Intent intent2 = new Intent();
        intent2.setAction("com.feng.restart");
        PendingIntent broadcast1 = PendingIntent.getBroadcast(this, 112, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
        remoteViews.setOnClickPendingIntent(R.id.notify_restart,broadcast1);


        builder.setCustomContentView(remoteViews);


        Notification build = builder.build();

        //前台服务
        startForeground(1,build);


        return super.onStartCommand(intent, flags, startId);
    }



    public void play(int position){

        index = position;
        //恢复成初始化的状态
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(musicList.get(position).getData());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });


            //播放完成的时候回调
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {

                    if(MusicModel.ORDER == model){
                        playNext();
                    }else if(MusicModel.RANDOM == model){
                        //随机数的产生
                        int i = new Random().nextInt(musicList.size());
                        play(i);
                    }else if(MusicModel.SINGLE == model){
                        play(index);
                    }

                }
            });


        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public void playNext(){
        //播放不同的下标

        if(++index >= musicList.size()-1){
            index = 0;
        }
        play(index);
    }

    public void palyPre(){
        if(--index <= 0){
            index = 0;
        }
        play(index);
    }


    public void pause(){
        if(mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }

    }

    public void restart(){
        mediaPlayer.start();
    }

}
